package com.zeta.eduservice.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeta.commonutils.R;
import com.zeta.eduservice.entity.EduStudent;
import com.zeta.eduservice.entity.EduTeacher;
import com.zeta.eduservice.entity.vo.StudentQuery;
import com.zeta.eduservice.entity.vo.TeacherQuery;
import com.zeta.eduservice.service.EduStudentService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 学生 前端控制器
 * </p>
 *
 * @author zeta
 * @since 2020-08-16
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/edu_student")
public class EduStudentController {

	@Autowired
	EduStudentService studentService;
	
	
	@ApiOperation("增加学生")
	@PostMapping("")
	public R addTeacher(@RequestBody EduStudent student) {
		boolean flag = studentService.save(student);
		if(flag)
			return R.ok();
		else
			return R.error();
	}
	@ApiOperation(value = "根据ID删除学生")
	@DeleteMapping("{id}")
	public R removeTeacher(@PathVariable String id) {
		boolean flag = studentService.removeById1(id);
		 if(flag){
			 EduStudent byId = studentService.getById(id);
		        return R.ok();
		    }else{
		        return R.error().message("删除失败");
		    }
	}

	@ApiOperation(value = "根据ID修改学生")
	@PutMapping("{id}")
	public R updateById(
	    @ApiParam(name = "id", value = "学生ID", required = true)
	    @PathVariable String id,

	    @ApiParam(name = "student", value = "学生对象", required = true)
	    @RequestBody EduStudent student){

		student.setId(id);
	    studentService.updateById(student);
	    return R.ok();
	}
	
	
	@ApiOperation("查询学生")
	@GetMapping("{id}")
	public R findTeacher(@PathVariable String id) {
		EduStudent byId = studentService.getById(id);
		return R.ok().data("student", byId);
	}
	@ApiOperation("查询所有教师")
	@GetMapping("/findAll")
	public R findAllTeacher() {
		List<EduStudent> list = studentService.list(null);
		return R.ok().data("students", list);

	}
	@ApiOperation(value = "分页查询学生列表")
	@GetMapping("{page}/{limit}")
	public R pageList(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,

			@ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit) {

		Page<EduStudent> pageParam = new Page<>(page, limit);
		studentService.page(pageParam, null);
		List<EduStudent> records = pageParam.getRecords();
		long total = pageParam.getTotal();
		return R.ok().data("total", total).data("rows", records);
	}

	@ApiOperation("条件查询带分页")
	@PostMapping("/pageStudentCondition/{current}/{limit}")//requestbody必须用 post   required 这个值可以没用
	public R PageTeacherCondition(@PathVariable long current, @PathVariable long limit,@RequestBody(required = false) StudentQuery     query) {
		Page<EduStudent> page = new Page<>(current, limit);
		QueryWrapper<EduStudent> wrapper = new QueryWrapper<EduStudent>();
		String name = query.getName();
		String classs=query.getClasss();
		String major=query.getMajor();
		String academu=query.getAcademu();
		String begin=query.getBegin();
		String end = query.getEnd();
		if(!StringUtils.checkValNull(name)) {
			wrapper.like("name", name);//模糊查询
		}
		if(classs!=null) {
			wrapper.eq("classs",classs);
		}
		if(major!=null) {
			wrapper.eq("major",major);
		}
		
		if(academu!=null) {
			wrapper.eq("academu",academu);
		}
		if(!StringUtils.checkValNull(begin)) {
			wrapper.ge("gmt_create", begin);
		}
		if(!StringUtils.checkValNull(end)) {
			wrapper.le("gmt_create", end);
		}
		
		studentService.page(page, wrapper);
		List<EduStudent> records = page.getRecords();
		long total = page.getTotal();
		return R.ok().data("total", total).data("rows", records);
	}

	
}

